/**
 * Copyright 2016 JustWayward Team
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ecy.quadratic.manager;

import com.ecy.quadratic.reader.base.Constant;
import com.ecy.quadratic.reader.bean.Recommend;
import com.ecy.quadratic.reader.utils.ACache;
import com.ecy.quadratic.reader.utils.FileUtils;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * 收藏列表管理
 * Created by lfh on 2016/9/22.
 */
public class LikeManager {

    private volatile static LikeManager singleton;

    private LikeManager() {

    }

    public static LikeManager getInstance() {
        if (singleton == null) {
            synchronized (LikeManager.class) {
                if (singleton == null) {
                    singleton = new LikeManager();
                }
            }
        }
        return singleton;
    }

    /**
     * 获取收藏列表
     *
     * @return
     */
    public List<String> getCollectionList() {
        List<String> list = (ArrayList<String>) ACache.get(new File(Constant.PATH_LIKE)).getAsObject("like");
        return list == null ? null : list;
    }

    public void putCollectionList(List<String> list) {
        ACache.get(new File(Constant.PATH_LIKE)).put("like", (Serializable) list);
    }


    /**
     * 是否已收藏
     *
     * @param bookId
     * @return
     */
    public boolean isCollected(String bookId) {
        List<String> list = getCollectionList();
        if (list == null || list.isEmpty()) {
            return false;
        }
        for (String bean : list) {
            if (bean.equals(bookId)) {
                return true;
            }
        }
        return false;
    }


    /**
     * 加入收藏
     *
     * @param bean
     */
    public boolean add(String bean) {
        if (isCollected(bean)) {
            return false;
        }
        List<String> list = getCollectionList();
        if (list == null) {
            list = new ArrayList<>();
        }
        list.add(bean);
        putCollectionList(list);
        return true;
    }


    public void clear() {
        try {
            FileUtils.deleteFileOrDirectory(new File(Constant.PATH_COLLECT));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 自定义比较器：按最近阅读时间来排序，置顶优先，降序
     */
    static class RecentReadingTimeComparator implements Comparator {
        public int compare(Object object1, Object object2) {
            Recommend.RecommendBooks p1 = (Recommend.RecommendBooks) object1;
            Recommend.RecommendBooks p2 = (Recommend.RecommendBooks) object2;
            if (p1.isTop && p2.isTop || !p1.isTop && !p2.isTop) {
                return p2.recentReadingTime.compareTo(p1.recentReadingTime);
            } else {
                return p1.isTop ? -1 : 1;
            }
        }
    }

    /**
     * 自定义比较器：按更新时间来排序，置顶优先，降序
     */
    static class LatelyUpdateTimeComparator implements Comparator {
        public int compare(Object object1, Object object2) {
            Recommend.RecommendBooks p1 = (Recommend.RecommendBooks) object1;
            Recommend.RecommendBooks p2 = (Recommend.RecommendBooks) object2;
            if (p1.isTop && p2.isTop || !p1.isTop && !p2.isTop) {
                return p2.updated.compareTo(p1.updated);
            } else {
                return p1.isTop ? -1 : 1;
            }
        }
    }

}